import 'package:flutter/material.dart';

void main(List<String> args) {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Scaffold(body: MyAppBody()),
    );
  }
}

class MyAppBody extends StatefulWidget {
  const MyAppBody({super.key});

  @override
  State<MyAppBody> createState() => _MyAppBodyState();
}

class _MyAppBodyState extends State<MyAppBody> {
  int _progress = 0;
  @override
  Widget build(BuildContext context) {
    return demo();
  }

  Widget demo() {
    return NotificationListener(
        child: demoContent(),
        onNotification: (ScrollNotification notification) {
          // 1.判断监听事件的类型
          if (notification is ScrollStartNotification) {
            print('开始滚动');
          } else if (notification is ScrollUpdateNotification) {
            final currentPixel = notification.metrics.pixels;
            final totalPixel = notification.metrics.maxScrollExtent;
            double progress = currentPixel / totalPixel;
            setState(() {
              _progress = (progress * 100).toInt();
            });
            print('正在滚动：$currentPixel ----- $totalPixel');
          } else if (notification is ScrollEndNotification) {
            print('结束滚动');
          }
          return false;
        });
  }

  Widget demoContent() {
    return Stack(
      alignment: const Alignment(0.9, 0.9),
      children: [
        ListView.builder(
            itemCount: 100,
            itemBuilder: (BuildContext ctx, int index) {
              return ListTile(
                title: Text('主题$index'),
              );
            }),
        CircleAvatar(
          radius: 50,
          backgroundColor: Colors.amber[900],
          child: Text('$_progress%'),
        )
      ],
    );
  }
}
